package org.yinweichen.llm_dataset_backend.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yinweichen.llm_dataset_backend.entity.LlmAnswer;
import org.yinweichen.llm_dataset_backend.repository.LlmAnswerRepository;
import org.yinweichen.llm_dataset_backend.service.LlmService;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class LlmServiceImpl implements LlmService {
    private final LlmAnswerRepository llmAnswerRepository;

    @Override
    public ResponseEntity<List<LlmAnswer>> getLlmAnswers(String modelName, Long questionId, String version) {
        List<LlmAnswer> answers;
        // 参数校验
        if (modelName == null && questionId == null && version == null) {
            answers = llmAnswerRepository.findAll();
        } else if (modelName != null && questionId != null && version != null) {
            // 同时提供modelName、questionId和version
            answers = llmAnswerRepository.findByModelAndQuestionIdAndVersion(modelName, questionId, version);
        } else if (modelName != null && questionId != null) {
            // 同时提供modelName和questionId
            answers = llmAnswerRepository.findByModelAndQuestionId(modelName, questionId);
        } else if (modelName != null && version != null) {
            // 同时提供modelName和version
            answers = llmAnswerRepository.findByModelAndVersion(modelName, version);
        } else if (questionId != null && version != null) {
            // 同时提供questionId和version
            answers = llmAnswerRepository.findByQuestionIdAndVersion(questionId, version);
        } else if (modelName != null) {
            // 只提供modelName
            answers = llmAnswerRepository.findByModel(modelName);
        } else if (questionId != null) {
            // 只提供questionId
            answers = llmAnswerRepository.findByQuestionId(questionId);
        } else {
            // 只提供version
            answers = llmAnswerRepository.findByVersion(version);
        }

        return ResponseEntity.ok(answers);
    }
}